package dao

import (
	"album/model"
	"context"
	"listenboos-common/db"
	"listenboos-common/db/gorms"
	"listenboos-common/tools"
	"time"
)

type AlbumAttributeValueDao struct {
	conn *gorms.GormConn
}

func (u *AlbumAttributeValueDao) Save(ctx context.Context, conn db.DBConn, id int64, data []*model.AlbumAttributeValue) error {
	u.conn = conn.(*gorms.GormConn)
	tx := u.conn.Tx(ctx)
	now := tools.ToTimeString(time.Now().UnixMilli())
	for _, d := range data {
		d.AlbumId = id
		d.CreateTime = now
		d.UpdateTime = now
	}
	err := tx.Save(&data).Error
	return err
}
func (u *AlbumAttributeValueDao) FindByAid(ctx context.Context, aid int64) ([]*model.AlbumAttributeValue, error) {
	session := u.conn.Session(ctx)
	var values []*model.AlbumAttributeValue
	err := session.Model(&model.AlbumAttributeValue{}).Where("album_id =?", aid).Find(&values).Error
	if err != nil {
		return nil, err
	}
	return values, nil
}
func (u *AlbumAttributeValueDao) DeleteAlbumAttributeValues(ctx context.Context, conn db.DBConn, albumId int64) error {
	data := model.AlbumAttributeValue{}
	u.conn = conn.(*gorms.GormConn)
	tx := u.conn.Tx(ctx)
	now := tools.ToTimeString(time.Now().UnixMilli())
	data.UpdateTime = now
	err := tx.Model(&model.AlbumAttributeValue{}).Where("album_id = ?", albumId).Update("is_deleted", 1).Error
	return err
}
func NewAlbumAttributeValueDao(db *db.ListenDB) *AlbumAttributeValueDao {
	return &AlbumAttributeValueDao{
		conn: gorms.New(db.Conn),
	}
}
